package com.ynet.middleground.riskmanage.service.impl;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.riskmanage.bean.hangxin.*;
import com.ynet.middleground.riskmanage.bean.hangxin.hzfpsqdsq.INPUT;
import com.ynet.middleground.riskmanage.dto.hangxin.*;
import com.ynet.middleground.riskmanage.model.hangxin.HxModel;
import com.ynet.middleground.riskmanage.service.HangXinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBException;
import java.util.List;

/**
 * @ClassName HangXinServiceImpl
 * @Description 对接航信服务实现类
 * @Author songyf
 * @Date 2021/11/05
 * @Version 1.0
 */
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Api(value = "电子发票接口服务", tags = {"电子发票接口服务"})
@Path("hangxin")
@Service(timeout = 6000, version = "0.0.1")
public class HangXinServiceImpl implements HangXinService, GenericService {

    @Autowired
    private HxModel hxModel;

    /**
     * @param req
     * @return
     * @throws JAXBException
     * @auth songyf
     * @desc 发票开具
     */
    @Override
    @Path("invoice-issued")
    @ApiOperation(value = "发票开具", notes = "发票开具", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "InvoiceIssuedReq", paramType = "body", required = true)
    @POST
    public ServiceResult<InvoiceIssuedDTO> invoiceIssued(@Valid InvoiceIssuedReq req) {
        ServiceResult<InvoiceIssuedDTO> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.invoiceIssued(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "发票开具服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 查询开票结果
     *
     * @param req
     * @auther songyf
     */
    @Path("query-invoice-result")
    @ApiOperation(value = "查询开票结果", notes = "查询开票结果", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "QueryInvoiceResultReq", paramType = "body", required = true)
    @Override
    @POST
    public ServiceResult<List<InvoiceResultDTO>> queryInvoiceResult(@Valid QueryInvoiceResultReq req) {
        ServiceResult<List<InvoiceResultDTO>> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.queryInvoiceResult(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "获取开票信息服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("HX0001", "服务异常，请联系系统管理员");
        }
        return result;
    }

    // /**
    // * 打印发票
    // *
    // * @param req
    // * @auther songyf
    // */
    // @Path("invoice-print")
    // @ApiOperation(value = "发票打印(51开票助手使用)", notes = "发票打印(51开票助手使用)", hidden = true)
    // @ApiImplicitParam(name = "requestObj", dataType = "InvoicePrintReq", paramType = "body", required = true)
    // @Override
    // @POST
    // public ServiceResult<String> invoicePrint(@Valid InvoicePrintReq req) {
    // ServiceResult<String> result = new ServiceResult<>();
    // try {
    // result.setResult(hxModel.invoicePrint(req));
    // } catch (BusinessException be) {
    // result.setError(be.getCode(), be.getMessage());
    // } catch (Exception e) {
    // IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "发票打印服务异常, case: {}",
    // Throwables.getStackTraceAsString(e));
    // result.setError("HX0001", "服务异常，请联系系统管理员");
    // }
    // return result;
    // }

    /**
     * @param req
     * @return
     * @throws JAXBException
     * @auth songyf
     * @desc 发票打印数据获取
     */
    @Override
    @Path("invoice-print-get")
    @ApiOperation(value = "发票打印数据获取(非51开票助手使用)", notes = "发票打印数据获取(非51开票助手使用)", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "InvoicePrintGetReq", paramType = "body", required = true)
    @POST
    public ServiceResult<InvoicePrintGetDTO> invoicePrintGet(@Valid InvoicePrintGetReq req) {
        ServiceResult<InvoicePrintGetDTO> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.invoicePrintGet(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "发票打印数据获取服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * @param req
     * @return
     * @throws JAXBException
     * @auth songyf
     * @desc 纸票作废
     */
    @Override
    @Path("invoice-void")
    @ApiOperation(value = "纸票作废", notes = "纸票作废", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "YzpzfReq", paramType = "body", required = true)
    @POST
    public ServiceResult<String> zpzf(@Valid YzpzfReq req) {
        ServiceResult<String> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.invoiceVoid(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "纸票作废服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * @param req
     * @return
     * @throws JAXBException
     * @auth songyf
     * @desc 纸票作废结果查询
     */
    @Override
    @Path("invoice-void-result")
    @ApiOperation(value = "纸票作废结果查询", notes = "纸票作废结果查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "YzpzfcxReq", paramType = "body", required = true)
    @POST
    public ServiceResult<String> zpzfcx(@Valid YzpzfcxReq req) {
        ServiceResult<String> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.invoiceVoidResult(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "纸票作废结果查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 云盒子库存查询
     *
     * @param req
     * @return
     */
    @POST
    @Path("ykccx")
    @ApiOperation(value = "云盒子库存查询", notes = "云盒子库存查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "YkccxReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<YkccxDTO>> ykccx(@Valid YkccxReq req) {
        ServiceResult<List<YkccxDTO>> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.ykccx(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "云盒子库存查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("HX0001", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 红字发票申请单申请
     *
     * @param req
     * @return
     */
    @POST
    @Path("hzfpsqdsq")
    @ApiOperation(value = "红字发票申请单申请", notes = "红字发票申请单申请（返回信息表流水号）", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "INPUT", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> hzfpsqdsq(@Valid INPUT req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.hzfpsqdsq(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "红字发票申请单申请服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("HX0001", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 红字发票申请单上传
     *
     * @param req
     * @return
     */
    @POST
    @Path("hzfpsqdsc")
    @ApiOperation(value = "红字发票申请单上传", notes = "红字发票申请单上传", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "HzfpsqdscReq", paramType = "body", required = true)
    @Override
    public ServiceResult<HzfpsqdscDTO> hzfpsqdsc(@Valid HzfpsqdscReq req) {
        ServiceResult<HzfpsqdscDTO> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.hzfpsqdsc(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "红字发票申请单上传服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("HX0001", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 红字发票申请单撤销
     *
     * @param req
     * @return
     */
    @POST
    @Path("hzfpsqdcx")
    @ApiOperation(value = "红字发票申请单撤销", notes = "红字发票申请单撤销", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "HzfpsqdcxReq", paramType = "body", required = true)
    @Override
    public ServiceResult<HzfpsqdcxDTO> hzfpsqdcx(@Valid HzfpsqdcxReq req) {
        ServiceResult<HzfpsqdcxDTO> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.hzfpsqdcx(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "红字发票申请单撤销服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("HX0001", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 红字发票申请单下载
     *
     * @param req
     * @return
     */
    @POST
    @Path("hzfpsqdxz")
    @ApiOperation(value = "红字发票申请单下载", notes = "红字发票申请单下载", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "HzfpsqdxzReq", paramType = "body", required = true)
    @Override
    public ServiceResult<HzfpsqdxzDTO> hzfpsqdxz(@Valid HzfpsqdxzReq req) {
        ServiceResult<HzfpsqdxzDTO> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.hzfpsqdxz(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "红字发票申请单下载服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("HX0001", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 税控业务操作处理结果查询
     *
     * @param req
     * @return
     */
    @POST
    @Path("operationResultQuery")
    @ApiOperation(value = "税控业务操作处理结果查询", notes = "税控业务操作处理结果查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "OperationResultQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<OperationResultQueryDTO> operationResultQuery(@Valid OperationResultQueryReq req) {
        ServiceResult<OperationResultQueryDTO> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.operationResultQuery(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "税控业务操作处理结果查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("HX0001", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 单据删除
     * 
     * @param req
     * @return
     */
    @POST
    @Path("djsc")
    @ApiOperation(value = "单据删除",
        notes = "单据删除<br/>场景介绍：<br/>1.单据未开票、单据开票失败、单据已经开票但是做了冲红或者作废，可以删除，返回00000<br/>2.单据未开票，但是已做拆分或者合并处理，不可以删除，返回10001<br/>3.单据开票中，不可以删除，返回10002<br/>4.单据已开票，不可以删除，返回10003",
        hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "InvoiceDeleteReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> djsc(@Valid InvoiceDeleteReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            result.setResult(hxModel.djsc(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "单据删除服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("HX0001", "服务异常，请联系系统管理员");
        }
        return result;
    }

}
